<template>
  <div class="login_container">
    <div class="login_box">
        <div class="logo_box">
        <img src="../assets/logo.png" alt="">
        </div>

        <div class="login_form">
           <el-form ref="myform" :model="loginForm" :rules="rules" class="demo-ruleForm">
                <el-form-item   prop="username">
                    <label for="">用户名</label>
                <el-input  v-model="loginForm.username" ></el-input>
                </el-form-item>

                <el-form-item  prop="password">
                    <label for="">密码</label>
                <el-input type='password' v-model="loginForm.password"  placeholder="密码6到8位"></el-input>
                </el-form-item>

                 <el-form-item class="btns">
                    <el-button   type="primary" @click="tologin">登录</el-button>
                    <el-button @click="reset">重置</el-button>
                 </el-form-item>
                 
            </el-form>
        </div>
    </div>
  </div>
</template>

<script>
export default {
    data(){
        return{
            loginForm:{
                username: 'admin',
                password: '123456'
            },
            rules:{
                username:[
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                ],
                password:[
                    { min: 6, max: 8, message: '密码6到8位', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
       tologin(){
            // alert('登陆成功');
            this.$refs.myform.validate(async (ok)=>{
                if(ok){
                   let ret =await this.$http.post('/login' ,this.loginForm);
                   console.log(ret);
                   console.log(this.loginForm);

                   if(ret.meta.status !==200){
                    // Message.success('登录失败');
                    this.$message.error(ret.meta.msg);
                    return
                   }
                   
                    this.$message({
                        message: '登录成功',
                        type: 'success'
                    });
                   //保存token
                   let tk=ret.data.token
                   localStorage.setItem('token',tk);
                   
                   this.$router.push('/home');
                }else{
                    this.$message({
                        message: '用户名或密码格式错误',
                        type: 'warning'
                    });
                }
            })
        },
        reset(){
            this.$refs.myform.resetFields();
        },
         open1() {
        this.$notify({
          title: '成功',
          message: '这是一条成功的提示消息',
          type: 'success'
        });
      },
    }

}
</script>

<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
.logo_box{
    height: 130px;
    width: 130px;
    border: solid 1px #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    border-block-color: #fff;
    transform: translate(-50%,-50%);
    img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}
.login_form{
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content: flex-end;
}
}

</style>